﻿#include "process_logic.h"

process_logic::process_logic(QObject *parent) : QObject(parent)
{
    process_logic_thread = std::thread(&process_logic::process_logic_task,this);
}

void process_logic::process_logic_triggered(QByteArray& buf)
{

    q.push(buf);
    data_ready = true;
    cv.notify_all();

}




void process_logic::process_logic_task()
{

    while(1)
    {
        std::unique_lock<std::mutex> lock(mtx);
        while(!data_ready)
        {
            cv.wait(lock);
        }
        data_ready = false;
        QByteArray buf = q.front();
        q.pop();

        QByteArray head = buf.mid(0,2);             //2
        QByteArray len_t = buf.mid(2,2);              //2
        QByteArray devaddr_t = buf.mid(4,2);          //2

        QByteArray funcode_t = buf.mid(6,1);          //1
        uint16_t len = len_t.at(0)<<8|len_t.at(1);
        uint16_t devaddr = devaddr_t.at(0)<<8|devaddr_t.at(1);
        uint8_t funcode = funcode_t.at(0);
        temp = buf.mid(7,len-1);   //4096
        QByteArray chk = buf.mid(6+len,2);    //2
        QByteArray tailer = buf.mid(8+len,2); //2
        if(devaddr == 0x5206)
        {
            switch(funcode)
            {
                case 0x41:
                if(len == 4096*2+1)
                {
                    emit get_display_data(&temp);
                }
                break;
                default: break;
            }
        }
        else
        {
            qDebug()<<"protocol not support";
        }
    }
}
